//
//  FlowAPI.swift
//  Employee
//
//  Created by swordjoy on 2017/6/27.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import Moya

let FlowProvider = MoyaProvider<FlowAPI>(
   manager: DefaultAlamofireManager.sharedManager,
   plugins:[
//                                                    NetworkLoggerPlugin(verbose: true)
  ]
)

public enum FlowAPI {
  case getFlowType
  case addFlow(model: FlowSend)
  case getFlowTypeDetail(id: String)
  case getApplyList(model: FlowListSend)
  case getCheckList(model: FlowListSend)
  case getCopyList(model: FlowListSend)
  case getApplyDetail(id: String)
  case checkApply(model: CheckSend)
  case myCheck(page: String)
  case removeFlow(id: String)
}

extension FlowAPI: TargetType {
  
    public var headers: [String : String]? {
        return GlobalConfig.httpHeaders
    }
  /// The path to be appended to `baseURL` to form the full `URL`.
  public var path: String {
    switch self {
    case .getFlowType:
      return "/ms/apply/flow/list.json"
    case .addFlow:
      return "/ms/apply/add.json"
    case .getFlowTypeDetail:
      return "/ms/apply/flow/detail.json"
    case .getApplyList:
      return "/ms/apply/myApply/listpage.json"
    case .getCheckList:
      return "/ms/apply/listpage.json"
    case .getCopyList:
      return "/ms/apply/copyMe/listpage.json"
    case .getApplyDetail:
      return "/ms/apply/detail.json"
    case .checkApply:
      return "/ms/apply/audit.json"
    case .myCheck:
      return "/ms/apply/approverList.json"
    case .removeFlow:
      return "/ms/apply/withdraw.json"
    }
  }
  
  
  /// The parameters to be incoded in the request.
  public var parameters: [String : Any] {
    switch self {
    case .getFlowType:
      return ["access_token": Token.shared.token]
    case let .addFlow(model):
      return ["access_token": Token.shared.token, "flowId": model.flowId, "flowType": model.flowType, "result": model.result, "fileIds": model.fileIds, "approvalIds": model.approvalIds]
    case let .getFlowTypeDetail(id):
      return ["access_token": Token.shared.token, "id": id]
    case let .getApplyList(model):
      return ["access_token": Token.shared.token, "flowTypeId": model.flowId, "page.pn": model.page, "page.size": 20, "status": model.status]
    case let .getCheckList(model):
      return ["access_token": Token.shared.token, "flowTypeId": model.flowId, "page.pn": model.page, "page.size": 20, "status": model.status]
    case let .getCopyList(model: model):
      return ["access_token": Token.shared.token, "flowTypeId": model.flowId, "page.pn": model.page, "page.size": 20, "status": model.status]
    case let .getApplyDetail(id):
      return ["access_token": Token.shared.token, "id": id]
    case let .checkApply(model):
      return ["access_token": Token.shared.token, "explain": model.explain, "type": model.type, "id": model.id]
    case let .myCheck(page):
      return ["access_token": Token.shared.token, "fields": "approvalName,approverStatus,createTime,id,title,flowType", "page.pn": page, "page.size": 20]
    case let .removeFlow(id):
      return ["access_token": Token.shared.token, "id": id]
    }
  }
  
  /// The target's base `URL`.
  public var baseURL: URL {
    return URL(string: GlobalConfig.BASE_OUT_URL)!
  }
  
  /// The HTTP method used in the request.
  public var method: Moya.Method {
    switch self {
    case .addFlow:
      return .post
    case .checkApply, .removeFlow:
      return .put
    default:
      return .get
    }
    
  }
  
  public var task: Task {
    return .requestParameters(parameters: self.parameters.signParame(GlobalConfig.privateKey) , encoding: URLEncoding.default)
  }
  
  /// The method used for parameter encoding.
  public var parameterEncoding: ParameterEncoding {
    return URLEncoding.default
  }
  
  // 测试使用,暂时用不上
  public var sampleData: Data {
    return Data()
  }
  
}
